//
//  BK_my_ShippingAddressServer.m
//  bestkeep
//
//  Created by bj on 2016/11/10.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BK_my_ShippingAddressServer.h"
#import "BK_my_ShippingAddress.h"
#import "RequestManager.h"

@implementation BK_my_ShippingAddressServer

NSString * const formalEnvironmentstrBKAPI = @"http://api.bestkeep.cn/";

NSString * const shippingAddressStrBKAPI = @"http://api.test.bestkeep.cn/";

//收货地址列表
NSString * const strShipping_address_list = @"deliver-address/list";

//设置默认收货地址
NSString * const strShipping_default_address = @"deliver-address/setdefault";

//删除收获地址
NSString * const strShipping_delete = @"deliver-address/delete";

//编辑收货地址
NSString * const strShipping_edit = @"deliver-address/edit";

//添加收获地址
NSString * const strShipping_add = @"deliver-address/add";

//获取用户默认地址
NSString * const strDefault_address = @"deliver-address/default";

/*
 * 判断是否是线上环境
 */
+ (NSString *)formalEnvironment:(NSString *)url {
    if (ApplicationEnvironmentProduction == 1) {
        return [formalEnvironmentstrBKAPI stringByAppendingString:url];
    }
    return [shippingAddressStrBKAPI stringByAppendingString:url];
}
/*
 * 收货地址地址列表
 接口地址：	deliver-address/list
 输入参数：	无
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 "data":
 {
 "list":
 [
 {
 "id": "xxx//收货地址ID",
 "deliver_name": "xxx//收货人姓名",
 "province_name": "xxx//省份名称",
 "city_name": "xxx//城市名称",
 "county_name": "xxx//区县名称",
 "address": "xxx//详细地址",
 "post_code": "xx//邮编",
 "is_defaul": "xxx//是否默认地址",
 "telephone": "xxx//手机电话",
 "fixed_telephone": "xxx//固定电话",
 "province_code": "xxx//省份代码",
 "city_code": "xxx//城市代码",
 "county_code": "xxx//区县代码",
 }
 ]
 }
 }
 */
+ (void)shippingAddressList:(ShippingAddressCallback)callback {
    NSString *url = [self formalEnvironment:strShipping_address_list];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:nil success:^(id  _Nullable responseObject) {
        NSString *success = [[responseObject objectForKey:@"success"] stringValue];
        if ([success isEqualToString:@"1"]) {
            if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dict = [responseObject objectForKey:@"data"];
                if (dict.count != 0) {
                    NSMutableArray *list_Array = [NSMutableArray array];
                    list_Array = [dict objectForKey:@"list"];
                    NSMutableArray *data_Array = [NSMutableArray array];
                    for (int i = 0; i < list_Array.count; i++) {
                        NSMutableDictionary *list_Dict = list_Array[i];
                        BK_my_ShippingAddress *address = [[BK_my_ShippingAddress alloc] init];
                        address.shippingAddressID = [list_Dict objectForKey:@"id"];
                        address.consigneeName = [list_Dict objectForKey:@"deliver_name"];
                        address.shippingProvinceName = [list_Dict objectForKey:@"province_name"];
                        address.shippingCityName = [list_Dict objectForKey:@"city_name"];
                        address.shippingCountyName = [list_Dict objectForKey:@"county_name"];
                        address.shippingDetailAddress = [list_Dict objectForKey:@"address"];
                        address.shippingPostCode = [list_Dict objectForKey:@"post_code"];
                        address.shippingAddressIsDefault = [list_Dict objectForKey:@"is_default"];
                        address.shippingTelephone = [list_Dict objectForKey:@"telephone"];
                        address.shippingFixedTelephone = [list_Dict objectForKey:@"fixed_telephone"];
                        address.shippingProvinceCode = [list_Dict objectForKey:@"province_code"];
                        address.shippingCityCode = [list_Dict objectForKey:@"city_code"];
                        address.shippingCountyCode = [list_Dict objectForKey:@"county_code"];
                        if ([address.shippingAddressIsDefault isEqualToString:@"1"]) {
                            address.isDefaultAddress = YES;
                        } else {
                            address.isDefaultAddress = NO;
                        }
                        [data_Array addObject:address];
                    }
                    callback(data_Array, nil);
                }
            }else{
                callback(responseObject[@"data"], nil);
            }
        
        } else {
            callback(responseObject, nil);
        }
    
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

/*
 * 设置默认收货地址
 接口名称：	deliver-address_setdefault
 接口用途：	用户设置默认的收货地址
 method：	POST
 接口地址：	deliver-address/setdefault
 输入参数：
 key	参数类型	是否必须	参数解释
 deliver_id	string	是	默认地址ID
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 }
 备注：
 请求参数：
 deliver_id:xxx//默认地址ID
 */
+ (void)setDefaultAddress:(NSDictionary *)parmeters :(ShippingAddressCallback)callback {
    NSString *url = [self formalEnvironment:strShipping_default_address];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:parmeters success:^(id  _Nullable responseObject) {
        callback(responseObject, nil);
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

/*
 * 删除收货地址
 接口名称：	deliver-address_delete
 接口用途：	用户删除收货地址
 method：	POST
 接口地址：	deliver-address/delete
 输入参数：
 key	参数类型	是否必须	参数解释
 deliver_id	string	是	默认地址ID
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 }
 备注：
 请求参数：
 deliver_id:xxx//默认地址ID
 */
+ (void)deleteShippingAddress:(NSDictionary *)parameters :(ShippingAddressCallback)callback {
    NSString *url = [self formalEnvironment:strShipping_delete];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:parameters success:^(id  _Nullable responseObject) {
        callback(responseObject, nil);
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

/*
 * 编辑收货地址
 接口名称：	deliver-address_edit
 接口用途：	用户编辑收货地址信息
 method：	POST
 接口地址：	deliver-address/edit
 输入参数：
 key	参数类型	是否必须	参数解释
 id	string	是	收货地址ID
 deliver_name	string	是	收货人姓名
 province_name	string	是	省份名称
 city_name	string	是	城市名称
 county_name	string	是	区县名称
 address	string	是	详细地址
 post_code	int	是	邮编
 is_default	string	是	是否默认地址
 telephone	int	是	手机电话
 fixed_telephone	int	是	固定电话
 province_code	int	是	省份代码
 city_code	int	是	城市代码
 county_code	int	是	区县代码
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 }
 备注：
 请求参数：
 data={ id:xxx//收货地址ID deliver_name:xxx//收货人姓名 province_name:xxx//省份名称 city_name:xxx//城市名称 county_name:xxx//区县名称 address:xxx//详细地址 post_code:xxx//邮编 is_default:xxx//是否默认地址 telephone:xxx//手机电话 fixed_telephone:xxx//固定电话 province_code:xxx//省份代码 city_code:xxx//城市代码 county_code:xxx//区县代码 }
 
 复制
 */
+ (void)editShippingAddress:(NSDictionary *)parameters :(ShippingAddressCallback)callback {
    NSString *url = [self formalEnvironment:strShipping_edit];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:parameters success:^(id  _Nullable responseObject) {
        callback(responseObject, nil);
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

/*
 * 添加收货地址
 接口用途：	用户添加收货地址
 method：	POST
 接口地址：	deliver-address/add
 输入参数：
 key	参数类型	是否必须	参数解释
 deliver_name	string	是	收货人姓名
 province_name	string	是	省份名称
 city_name	string	是	城市名称
 county_name	string	是	区县名称
 address	string	是	详细地址
 post_code	int	是	邮编
 is_default	boolean	是	是否默认地址
 telephone	int	是	手机电话
 fixed_telephone	int	是	固定电话
 province_code	int	是	省份代码
 city_code	int	是	城市代码
 county_code	int	是	区县代码
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 }
 */
+ (void)addShippingAddress:(NSDictionary *)dict callback:(ShippingAddressCallback)callback {
    NSString *url = [self formalEnvironment:strShipping_add];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:dict success:^(id  _Nullable responseObject) {
        callback(responseObject, nil);
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}

/*
 * 获取用户默认地址
 
 接口名称：	deliver-address_default
 接口用途：	获取用户默认的收货地址
 method：	POST
 接口地址：	deliver-address/default
 输入参数：	无
 返回数据：
 {
 "success": "true/false //成功/失败",
 "msg": "xxx //返回消息",
 "code": "xxx //返回编码",
 "data":
 {
 "deliver_id": "xxx//收货地址ID",
 "deliver_name": "xxx//收货人姓名",
 "deliver_mobile": "xxx//收货人手机号",
 "deliver_address": "xxxx//收货地址（完整地址）",
 }
 }
 */
+ (void)getUserDefaultShippingAddressCallback:(ShippingAddressCallback)callback {
    NSString *url = [self formalEnvironment:strDefault_address];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:url parameters:nil success:^(id  _Nullable responseObject) {
        callback(responseObject, nil);
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        callback(nil, error);
    }];
}


@end
